import 'dart:math';

import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:share_fit/app/models/home/workout_model.dart';

class MotionEndState {
  late WorkoutModel workoutModel;
  late Polyline polyLine;

  /// 运动中运动心率数据
  List<int> heartRateList = [];
  List hrMapList = [];

  /// 运动中配速数据
  List<int> paceList = [];
  List paceMapList = [];

  /// 运动中步频数据
  List<int> cadenceList = [];
  List cadenceMapList = [];

  /// 运动中海拔数据
  List<int> altitudeList = [];
  List altitudeMapList = [];

  ///
  List<Marker> markers = [Marker(markerId: MarkerId('start')), Marker(markerId: MarkerId('end'))];

  /// 记录上一个路由页面
  String? fromPage;

  int? personAge;

  MotionEndState() {
    ///Initialize variables
  }

  /// 最大心率计算=220-年龄
  /// 如果用户没有设置年龄，则在心率列表中找最大值
  get maxHeartRate {
    if (null != personAge && personAge! > 0) {
      return 220 - personAge!;
    }
    return heartRateList.isEmpty ? 0 : heartRateList.reduce(max);
  }

  get limitPercentText {
    int p = limitPercent.toInt();
    return p <= 0 ? '0%' : '$p%';
  }

  /// 极限(最大心率的89%-100%)
  get limitPercent {
    int maxHr = maxHeartRate;
    double min = maxHr * 0.89;

    int len = heartRateList.where((element) => element >= min && element <= maxHr).length;

    return heartRateList.isEmpty ? 0.0 : len / heartRateList.length * 100;
  }

  get endurancePercentText {
    int p = endurancePercent.toInt();
    return p <= 0 ? '0%' : '$p%';
  }

  /// 无氧耐力(最大心率的79%-89%)
  get endurancePercent {
    int maxHr = maxHeartRate;
    double min = maxHr * 0.79;
    double max = maxHr * 0.89;

    int len = heartRateList.where((element) => element >= min && element <= max).length;

    return heartRateList.isEmpty ? 0.0 : len / heartRateList.length * 100;
  }

  get anaerobicPercentText {
    int p = anaerobicPercent.toInt();
    return p <= 0 ? '0%' : '$p%';
  }

  /// 有氧耐力(最大心率的70%-79%)
  get anaerobicPercent {
    int maxHr = maxHeartRate;
    double min = maxHr * 0.7;
    double max = maxHr * 0.79;

    int len = heartRateList.where((element) => element >= min && element <= max).length;

    return heartRateList.isEmpty ? 0.0 : len / heartRateList.length * 100;
  }

  get fatPercentText {
    int p = fatPercent.toInt();
    return p <= 0 ? '0%' : '$p%';
  }

  /// 燃脂(最大心率的60%-70%)
  get fatPercent {
    int maxHr = maxHeartRate;
    double min = maxHr * 0.6;
    double max = maxHr * 0.7;

    int len = heartRateList.where((element) => element >= min && element <= max).length;

    return heartRateList.isEmpty ? 0.0 : len / heartRateList.length * 100;
  }

  get warmPercentText {
    int p = warmPercent.toInt();
    return p <= 0 ? '0%' : '$p%';
  }

  /// 热身(最大心率的50%-60%)
  get warmPercent {
    int maxHr = maxHeartRate;
    double min = maxHr * 0.5;
    double max = maxHr * 0.6;

    int len = heartRateList.where((element) => element >= min && element <= max).length;

    return heartRateList.isEmpty ? 0.0 : len / heartRateList.length * 100;
  }
}
